<?php
class ModelSurveySurvey extends Model {
	public function addSurvey($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "survey SET sort_order = '" . (int)$data['sort_order'] . "', mode = '" . $data['mode'] . "', progress = '" . $data['progress'] . "', login = '" . $data['login'] . "', status = '" . (int)$data['status'] . "'");

		$survey_id = $this->db->getLastId(); 
			
		foreach ($data['survey_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "survey_description SET survey_id = '" . (int)$survey_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', closing = '" . $this->db->escape($value['closing']) . "'");
		}
		
		if (isset($data['survey_store'])) {
			foreach ($data['survey_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "survey_to_store SET survey_id = '" . (int)$survey_id . "', store_id = '" . (int)$store_id . "'");
			}
		}

		if (isset($data['survey_layout'])) {
			foreach ($data['survey_layout'] as $store_id => $layout) {
				if ($layout) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "survey_to_layout SET survey_id = '" . (int)$survey_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
				}
			}
		}
				
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'survey_id=" . (int)$survey_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('survey');
	}
	
	public function editSurvey($survey_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "survey SET sort_order = '" . (int)$data['sort_order'] . "', mode = '" . $data['mode'] . "', progress = '" . $data['progress'] . "', login = '" . $data['login'] . "', status = '" . (int)$data['status'] . "' WHERE survey_id = '" . (int)$survey_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "survey_description WHERE survey_id = '" . (int)$survey_id . "'");
					
		foreach ($data['survey_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "survey_description SET survey_id = '" . (int)$survey_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', closing = '" . $this->db->escape($value['closing']) . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "survey_to_store WHERE survey_id = '" . (int)$survey_id . "'");
		
		if (isset($data['survey_store'])) {
			foreach ($data['survey_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "survey_to_store SET survey_id = '" . (int)$survey_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "survey_to_layout WHERE survey_id = '" . (int)$survey_id . "'");

		if (isset($data['survey_layout'])) {
			foreach ($data['survey_layout'] as $store_id => $layout) {
				if ($layout['layout_id']) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "survey_to_layout SET survey_id = '" . (int)$survey_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
				}
			}
		}
				
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'survey_id=" . (int)$survey_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'survey_id=" . (int)$survey_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('survey');
	}
	
	public function deleteSurvey($survey_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "survey WHERE survey_id = '" . (int)$survey_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "survey_description WHERE survey_id = '" . (int)$survey_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "survey_to_store WHERE survey_id = '" . (int)$survey_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "survey_to_layout WHERE survey_id = '" . (int)$survey_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'survey_id=" . (int)$survey_id . "'");

		$this->cache->delete('survey');
	}
	
	public function getNextSortOrder() {
		$sql = "SELECT max(sort_order) as sort_order FROM " . DB_PREFIX . "survey WHERE status=1"; // AND survey_id = '" . (int)$survey_id . "'
//		echo $sql; 
		$query = $this->db->query($sql);
		return $query->row['sort_order'] + 1; 
	}	

	public function getSurvey($survey_id) {
		
		$sql = "SELECT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'survey_id=" . (int)$survey_id . "') AS keyword, i.survey_id as survey_id FROM " . DB_PREFIX . "survey i LEFT JOIN " . DB_PREFIX . "survey_description id ON (i.survey_id = id.survey_id) WHERE i.survey_id = '" . (int)$survey_id . "' AND id.language_id = '" . (int)$this->config->get('config_language_id') . "'";		
		
		$query = $this->db->query($sql);
		
		return $query->row;
	}
		
	public function getSurveys($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "survey i LEFT JOIN " . DB_PREFIX . "survey_description id ON (i.survey_id = id.survey_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
			if (isset($data['filter_survey_ides']) && !is_null($data['filter_survey_ides'])) {
				$sql .= " AND i.survey_id in (" . $data['filter_survey_ides'] . ")";
			}
			
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND i.status = '" . (int)$data['filter_status'] . "'";
			}
		
			$sort_data = array(
				'i.survey_id',
				'id.title',
				'i.sort_order'
			);		
		
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY id.title";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}		

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
			
			return $query->rows;
		} else {
			$survey_data = $this->cache->get('survey.' . $this->config->get('config_language_id'));
		
			if (!$survey_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "survey i LEFT JOIN " . DB_PREFIX . "survey_description id ON (i.survey_id = id.survey_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY id.title");
	
				$survey_data = $query->rows;
			
				$this->cache->set('survey.' . $this->config->get('config_language_id'), $survey_data);
			}	
	
			return $survey_data;			
		}
	}
	
	public function getCustomerParticipents($data = array()) {
		
		$sql ="SELECT *, concat(c.firstname, ' ', c.lastname) as name, o.customer_id as customer_id FROM " . DB_PREFIX . "answer o LEFT JOIN " . DB_PREFIX . "customer c ON o.customer_id = c.customer_id WHERE 1=1 ";
		
		$sql .= " AND o.customer_id != '0' ";
		
		if (isset($data['filter_answer_id']) && !is_null($data['filter_answer_id'])) {
			$sql .= " AND o.answer_id = '" . (int)$data['filter_answer_id'] . "'";
		}
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND o.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		if (isset($data['filter_answer']) && !is_null($data['filter_answer'])) {
			switch($data['filter_answer']) {
				case 'not-empty' :
					$sql .= " AND trim(o.answer) != ''";
				break;
				default:
					$sql .= " AND o.answer = '" . $this->db->escape($data['filter_answer']) . "'";				
				break;				
			}
		}
		
		if (isset($data['filter_question_id']) && !is_null($data['filter_question_id'])) {
			$sql .= " AND o.question_id = '" . (int)$data['filter_question_id'] . "'";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}		

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
//		echo $sql;
				
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getGuestParticipents($data = array()) {
		
		$sql ="SELECT *, concat(g.firstname, ' ', g.lastname) as name, o.guest_id as guest_id FROM " . DB_PREFIX . "answer o LEFT JOIN " . DB_PREFIX . "guest g ON o.guest_id = g.guest_id WHERE 1=1 ";
		
		$sql .= " AND o.customer_id = '0' ";
		
		if (isset($data['filter_answer_id']) && !is_null($data['filter_answer_id'])) {
			$sql .= " AND o.answer_id = '" . (int)$data['filter_answer_id'] . "'";
		}
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND o.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		if (isset($data['filter_answer']) && !is_null($data['filter_answer'])) {
			switch($data['filter_answer']) {
				case 'not-empty' :
					$sql .= " AND trim(o.answer) != ''";
				break;
				default:
					$sql .= " AND o.answer = '" . $this->db->escape($data['filter_answer']) . "'";				
				break;				
			}
		}
		
		if (isset($data['filter_question_id']) && !is_null($data['filter_question_id'])) {
			$sql .= " AND o.question_id = '" . (int)$data['filter_question_id'] . "'";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}		

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
//		echo $sql;
				
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getParticipents($data = array()) {
		
		$sql ="SELECT *, concat(c.firstname, ' ', c.lastname) as name FROM " . DB_PREFIX . "answer o LEFT JOIN " . DB_PREFIX . "customer c ON o.customer_id = c.customer_id WHERE 1=1 ";
		
		if (isset($data['filter_customer_id']) && !is_null($data['filter_customer_id'])) {
			$sql .= " AND o.customer_id = '" . (int)$data['filter_customer_id'] . "'";
		}
		
		if (isset($data['filter_answer_id']) && !is_null($data['filter_answer_id'])) {
			$sql .= " AND o.answer_id = '" . (int)$data['filter_answer_id'] . "'";
		}
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND o.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		if (isset($data['filter_answer']) && !is_null($data['filter_answer'])) {
			switch($data['filter_answer']) {
				case 'not-empty' :
					$sql .= " AND trim(o.answer) != ''";
				break;
				default:
					$sql .= " AND o.answer = '" . $this->db->escape($data['filter_answer']) . "'";				
				break;				
			}
		}
		
		if (isset($data['filter_question_id']) && !is_null($data['filter_question_id'])) {
			$sql .= " AND o.question_id = '" . (int)$data['filter_question_id'] . "'";
		}
		
		$sql .= " GROUP BY o.customer_id ";
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}		

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
		
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
//		echo $sql;
				
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getAnswer($data = array()) {
		
		$sql ="SELECT * FROM " . DB_PREFIX . "answer o WHERE 1=1 ";
		
		if (isset($data['filter_answer_id']) && !is_null($data['filter_answer_id'])) {
			$sql .= " AND o.answer_id = '" . (int)$data['filter_answer_id'] . "'";
		}
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND o.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		if (isset($data['filter_customer_id']) && !is_null($data['filter_customer_id'])) {
			$sql .= " AND o.customer_id = '" . (int)$data['filter_customer_id'] . "'";
		}
		
		if (isset($data['filter_guest_id']) && !is_null($data['filter_guest_id'])) {
			$sql .= " AND o.guest_id = '" . (int)$data['filter_guest_id'] . "'";
		}
		
		if (isset($data['filter_answer']) && !is_null($data['filter_answer'])) {
			switch($data['filter_answer']) {
				case 'not-empty' :
					$sql .= " AND trim(o.answer) != ''";
				break;
				default:
					$sql .= " AND o.answer = '" . $this->db->escape($data['filter_answer']) . "'";				
				break;				
			}
		}
		
		if (isset($data['filter_question_id']) && !is_null($data['filter_question_id'])) {
			$sql .= " AND o.question_id = '" . (int)$data['filter_question_id'] . "'";
		}
		
//		echo $sql;
				
		$query = $this->db->query($sql);
		
		return $query->row;		
	
	}
	
	public function getTotalParticipent($data = array()) {
		
		$sql ="SELECT COUNT(DISTINCT email) AS total FROM " . DB_PREFIX . "answer o WHERE 1=1 ";
		
		if (isset($data['filter_answer_id']) && !is_null($data['filter_answer_id'])) {
			$sql .= " AND o.answer_id = '" . (int)$data['filter_answer_id'] . "'";
		}
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND o.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		if (isset($data['filter_customer_id']) && !is_null($data['filter_customer_id'])) {
			$sql .= " AND o.customer_id = '" . (int)$data['filter_customer_id'] . "'";
		}
		
		if (isset($data['filter_guest_id']) && !is_null($data['filter_guest_id'])) {
			$sql .= " AND o.guest_id = '" . (int)$data['filter_guest_id'] . "'";
		}
		
		if (isset($data['filter_answer']) && !is_null($data['filter_answer'])) {
			$sql .= " AND o.answer = '" . $this->db->escape($data['filter_answer']) . "'";
		}
		
		if (isset($data['filter_check_answer']) && !is_null($data['filter_check_answer'])) {
			switch($data['filter_check_answer']) {
				case 'not-empty' :
				$sql .= " AND trim(o.answer) != ''";
				break;				
			}
		}
		
		if (isset($data['filter_question_id']) && !is_null($data['filter_question_id'])) {
			$sql .= " AND o.question_id = '" . (int)$data['filter_question_id'] . "'";
		}
		
		if (isset($data['filtered_emails']) && !is_null($data['filtered_emails'])) {
			$filtered_emails = implode("','", $data['filtered_emails']);
			$sql .= " AND o.email in ('" . $filtered_emails . "') ";
		}
		
//		echo $sql;
				
		$query = $this->db->query($sql);
		
		return $query->row['total'];		
	}
	
	public function getTotalParticipentBySurveyId($survey_id) {
		
		$sql = "SELECT COUNT(DISTINCT email) AS total FROM " . DB_PREFIX . "answer WHERE survey_id = '" . (int)$survey_id . "'";
				
		$query = $this->db->query($sql);
		
		return $query->row['total'];		
	}
	
	public function getTotalGuestBySurveyId($survey_id) {

		$sql = "SELECT COUNT(DISTINCT a.email) AS total FROM " . DB_PREFIX . "answer a LEFT JOIN " . DB_PREFIX . "customer c ON a.customer_id = c.customer_id WHERE c.customer_group_id = '6' AND a.survey_id = '" . (int)$survey_id . "'";				
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];		
	}
	
	public function getTotalCustomerBySurveyId($survey_id) {
		
		$sql = "SELECT COUNT(DISTINCT a.email) AS total FROM " . DB_PREFIX . "answer a LEFT JOIN " . DB_PREFIX . "customer c ON a.customer_id = c.customer_id WHERE c.customer_group_id != '6' AND a.survey_id = '" . (int)$survey_id . "'";				
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];		
	}
	
	public function getSurveyDescriptions($survey_id) {
		$survey_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "survey_description WHERE survey_id = '" . (int)$survey_id . "'");

		foreach ($query->rows as $result) {
			$survey_description_data[$result['language_id']] = array(
				'title'       => $result['title'],
				'description' => $result['description'],
				'closing' => $result['closing']
			);
		}
		
		return $survey_description_data;
	}
	
	public function getSurveyStores($survey_id) {
		$survey_store_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "survey_to_store WHERE survey_id = '" . (int)$survey_id . "'");

		foreach ($query->rows as $result) {
			$survey_store_data[] = $result['store_id'];
		}
		
		return $survey_store_data;
	}

	public function getSurveyLayouts($survey_id) {
		$survey_layout_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "survey_to_layout WHERE survey_id = '" . (int)$survey_id . "'");
		
		foreach ($query->rows as $result) {
			$survey_layout_data[$result['store_id']] = $result['layout_id'];
		}
		
		return $survey_layout_data;
	}
		
	public function getTotalSurveys() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "survey");
		
		return $query->row['total'];
	}	
	
	public function getTotalSurveysByLayoutId($layout_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "survey_to_layout WHERE layout_id = '" . (int)$layout_id . "'");

		return $query->row['total'];
	}	
}
?>